import Vue from "vue";
import Vuex from 'vuex'
import request from "@/utils/request";

const user = {
  namespaced:true,
  actions:{
    async login(context,value){
      await request.post("/crop/user/login",value).then(
        response => {
          context.commit("Login",response)
        },
        error =>{
          console.log(error)
        })
    },

    async changeUser(context, value){
      await request.post("/crop/user/changeUser",value).then(
        response => {

        },
        error => {

        }
      )
    },

    async delUserById(context, value){
      await request.get("/crop/user/delUserById",{params:{id:value}})
    },

    async expertChecked(context,value){
      await request.get("/crop/user/expertChecked",{params:{id:value}})
    },

    async expertUnChecked(context,value){
      await request.get("/crop/user/expertUnChecked",{params:{id:value}})
    }
  },
  mutations:{
    Login(state,value){
      state.loginUser.msg = value.msg
      if (value.code === 1){
        state.loginUser.id = value.data.id
        state.loginUser.isLogin = true;
        state.loginUser.username = value.data.username
        state.loginUser.password = value.data.password
        state.loginUser.role = value.data.role
      }else{
        state.loginUser.isLogin = false;
      }
    }
  },
  state:{
    loginUser:{
      isLogin:false,
      msg:'',
      id:0,
      username:'',
      password:'',
      role:'',
    }
  },
  getters:{

  }
}

const crop = {
  namespaced: true,
  actions: {
    async findClass(context){
      await request.post('crop/crop/findAll',{}).then(
        response => {
          if (response.code === 1) context.commit('FindClass',response.data)
          else alert(response.msg)
        },
        error => {
          alert('后台出错!请通知管理员' + error)
        }
      )
    },
  },

  mutations: {
    FindClass(states, value){
      states.cropClass=[]
      value.forEach(function (item) {
        states.cropClass.push({
          cropId: item.id,
          cropName: item.crop_name
        })
      });
    }
  },

  state: {
    cropClass:null
  },

  getters: {

  }
}

const disease = {
  namespaced:true,
  actions:{
    async saveDis(context,value){
      let data = new FormData()
      data.append("id",value)
      await request.post('/crop/temporary/identify',data).then(
        response => {
          context.commit('SaveDis',response)
        },
        error => {
          alert("图片识别出错，请通知管理员！"+error)
        }
      )
    },

    async findDisByCropId(context,value){
      await request.get('/crop/disease/findDisByCropId', {params:{cropId:value}}).then(
        response => {
          context.commit('FindDisByCropId',response)
        },
        error => {
          console.log(error)
        }
      )
    },
    async findDisByText(context,value){
      await request.get('/crop/disease/findDisByText',{params:{text:value}}).then(
        response => {
          context.commit("FindDisByText",response)
        },
        error => {
          console.log(error)
        }
      )
    }
  },
  mutations:{
    SaveDis(state, value) {
      state.currentDis.push({
        id:value.id,
        crop_name:value.crop_name,
        disease_name:value.disease_name,
        disease_level:value.disease_level,
        disease_info:value.disease_info,
        disease_resolve:value.disease_resolve,
        isSave:false
      })
    },

    FindDisByCropId(state,value){
      state.disByCropId=[]
      value.forEach((item) => {
        state.disByCropId.push({
          disId:item.id,
          disName:item.disease_name,
          disease_info:item.disease_info,
          disease_resolve:item.disease_resolve
        })
      })
    },

    SetShowDisById(state,value){
      state.disByCropId.forEach((item) => {
        if (item.disId === value){
          state.showDis.isShow = true
          state.showDis.name = item.disName
          state.showDis.info = item.disease_info
          state.showDis.resolve = item.disease_resolve
        }
      })
    },

    FindDisByText(state,value){
      state.disSearchList=[]
      value.forEach((item) => {
        state.disSearchList.push({
          id:item.id,
          crop_name: item.crop_name,
          disease_info:item.disease_info,
          disease_name:item.disease_name,
          disease_resolve:item.disease_resolve
        })
      })
    },

    SetShowDis(state,value){
      state.showDis.isShow = true
      state.showDis.name = value.disease_name
      state.showDis.info = value.disease_info
      state.showDis.resolve = value.disease_resolve
    }
  },

  state:{
    currentDis:[],
    disByCropId:[],
    showDis:{
      isShow:false,
      name:'',
      info:'',
      resolve:''
    },
    disSearchList:[]
  }
}

const expert = {
  namespaced:true,
  actions:{
    async findExpert(context,value){
      let result = []
      await request.post("/crop/expert/findExpertBySearch",value).then(
        response => {
          result = response
          console.log(response)
        },
        error => {
          console.log(error)
        }
      )

      for (let item of result) {
        await request.get("/crop/user/findUserById",{params:{id:item.uid}}).then(
          response => {
            item.user=response
          },
          error => {
            console.log(error)
          }
        )
      }
      context.commit("FindExpert",result)
    },

    async findAllExpert(context){
      let result = []
      await request.post("/crop/expert/findAllExpert",{}).then(
        response => {
          result = response

        },
        error => {
          console.log(error)
        }
      )
      for (let item of result) {
        await request.get("/crop/user/findUserById",{params:{id:item.uid}}).then(
          response => {
            item.user=response
          },
          error => {
            console.log(error)
          }
        )
      }

      context.commit("FindAllExpert",result)
    },

    async changeExpert(context,value){
      await request.post("/crop/expert/changeExpert",value)
      value.id = value.uid
      await request.post("/crop/user/changeUser",value)
    },

    async findAllUncheckedExpert(context){
      await request.post("/crop/expert/findAllUncheckedExpert").then(
        response => {
          context.commit("FindAllUncheckedExpert",response)
        },
        error => {
          console.log(error)
        }
      )
    },

    async findUncheckedBySearch(context,value){
      await request.post("/crop/expert/findUncheckedBySearch",value).then(
        response => {
          context.commit("FindAllUncheckedExpert",response)
        },
        error => {
          console.log(error)
        }
      )
    },

    async delExpertById(context,value){
      await request.get("/crop/expert/delExpertById",{params:{id:value}})
    },

    async findExpertByUid(context){
      await request.get("/crop/expert/findExpertByUid",{params:{uid:this.state.user.loginUser.id}}).then(
        response => {
          context.commit("FindExpertByUid",response)
        },
        error => {
          console.log(error)
        }
      )
    },

    async changeExpertByExpert(context,value){
      await request.post("/crop/expert/changeExpertByExpert",value)
      value.id = value.uid
      await request.post("/crop/user/changeUser",value)
    },
  },

  mutations:{
    FindExpert(state, value) {
      state.showSearchList = value
    },
    SetShowExpert(state,value){
      state.showOneExpert = value
    },
    FindAllExpert(state,value){
      // state.allExpert = value
      state.showSearchList = value
    },
    FindAllUncheckedExpert(state,value){
      state.allUncheckedExpert = value
    },
    FindExpertByUid(state,value){
      state.loginExpert = value
    }
  },

  state:{
    showSearchList:[],
    showOneExpert:{},
    allExpert:[],
    allUncheckedExpert:[],
    loginExpert:{}
  }
}

const question = {
  namespaced:true,
  actions:{
    async findAllUnsolved(context,value){
      let result = []
      await request.get("/crop/question/findAllUnsolved",{params:{fid:value}}).then(
        response => {
          result = response
        },
        error => {
          console.log(error)
        }
      )
      for (let item of result) {
        await request.get("/crop/expert/findExpertById",{params:{id:item.eid}}).then(
          response => {
            item.farmer=response
          },
          error =>{
            console.log(error)
          }
        )
      }
      context.commit("FindAllUnsolved",result)
    },

    async findAllSolved(context,value){
      let result = []
      await request.get("/crop/question/findAllSolved",{params:{fid:value}}).then(
        response => {
          result = response
        },
        error => {
          console.log(error)
        }
      )
      for (let item of result) {
        await request.get("/crop/expert/findExpertById",{params:{id:item.eid}}).then(
          response => {
            item.farmer=response
          },
          error =>{
            console.log(error)
          }
        )
      }
      context.commit("FindAllSolved",result)
    },

    async delQuestion(context,value){
      await request.get("/crop/question/delQuestionById",{params:{id:value.id}}).then(
        response => {
          context.commit("DelQuestion",value)
        },
        error => {
          console.log(error)
        }
      )
    },

    async findAllUnsolvedByEid(context,value){
      let result = {}
      await request.get("/crop/question/findAllUnsolvedByEid",{params:{eid:value}}).then(
        response => {
          result = response
        },
        error =>{
          console.log(error)
        }
      )
      for (let item of result) {
        await request.get("/crop/farmer/findFarmerByFid",{params:{fid:item.fid}}).then(
          response => {
            item.farmer = response
          },
          error => {
            console.log(error)
          }
        )
      }
      context.commit("FindAllUnsolvedByEid",result)
    },

    async findAllSolvedByEid(context,value){
      let result = []
      await request.get("/crop/question/findAllSolvedByEid",{params:{eid:value}}).then(
        response => {
          result = response
        },
        error =>{
          console.log(error)
        }
      )
      for (let item of result) {
        await request.get("/crop/farmer/findFarmerByFid",{params:{fid:item.fid}}).then(
          response => {
            item.farmer = response
          },
          error => {
            console.log(error)
          }
        )
      }
      context.commit("FindAllSolvedByEid",result)
    },

    async answerQuestion(context,value){
      await request.post("/crop/question/answerQuestion",value)
    },

    async findUnresolvedQuestionByText(context,value){
      let result = []
      await request.post("/crop/question/findUnresolvedQuestionByText",value).then(
        response => {
          result = response
        },
        error => {
          console.log(error)
        }
      )

      for (let item of result) {
        await request.get("/crop/farmer/findFarmerByFid",{params:{fid:item.fid}}).then(
          response => {
            item.farmer = response
          },
          error => {
            console.log(error)
          }
        )
      }
      context.commit("FindAllUnsolvedByEid",result)
    },

    async findSolvedQuestionByText(context,value){
      let result = []
      await request.post("/crop/question/findSolvedQuestionByText",value).then(
        response => {
          result = response
        },
        error =>{
          console.log(error)
        }
      )
      for (let item of result) {
        await request.get("/crop/farmer/findFarmerByFid",{params:{fid:item.fid}}).then(
          response => {
            item.farmer = response
          },
          error => {
            console.log(error)
          }
        )
      }
      context.commit("FindAllSolvedByEid",result)
    }
  },
  mutations:{
    FindAllUnsolved(state, value){
      state.unsolved = value
    },

    FindAllSolved(state, value){
      state.solved = value
    },

    DelQuestion(state, value){
      if (value.state === 0){
        const index = state.unsolved.indexOf(value)
        state.unsolved.splice(index,1)
      }else{
        const index = state.solved.indexOf(value)
        state.solved.splice(index,1)
      }
    },

    FindAllUnsolvedByEid(state, value){
      state.unsolvedByEid = value
    },

    FindAllSolvedByEid(state, value){
      state.solvedByEid = value
    },
  },
  state:{
    unsolved:[],
    solved:[],
    unsolvedByEid:[],
    solvedByEid:[],
  }
}

const farmer = {
  namespaced:true,
  actions:{
    async findFarmerByUid(context){
      await request.get("/crop/farmer/findFarmerByUid",{params:{uid:this.state.user.loginUser.id}}).then(
        response => {
          context.commit("FindFarmerByUid",response)
        },
        error => {
          console.log(error)
        }
      )
    },

    async changeFarmer(context,value){
      await request.post("/crop/farmer/changeFarmer",value)
      value.id = value.uid
      await request.post("/crop/user/changeUser",value)
    },

    async findAllFarmer(context){
      let result = []
      await request.post("/crop/farmer/findAllFarmer",{}).then(
        response => {
          result = response
        },
        error => {
          console.log(error)
        }
      )

      for (let item of result) {
        await request.get("/crop/user/findUserById",{params:{id:item.uid}}).then(
          response => {
            item.user=response
          },
          error => {
            console.log(error)
          }
        )
      }

      context.commit("FindAllFarmer",result)
    },

    async findFarmerByText(context, value){
      let result = []
      await request.post("/crop/farmer/findFarmerByText",value).then(
        response => {
          result = response
        },
        error => {
          console.log(error)
        }
      )

      for (let item of result) {
        await request.get("/crop/user/findUserById",{params:{id:item.uid}}).then(
          response => {
            item.user=response
          },
          error => {
            console.log(error)
          }
        )
      }

      context.commit("FindAllFarmer",result)
    },

    async delFarmerById(context, value){
      await request.get("/crop/farmer/delFarmerById",{params:{id:value}})
    },
  },
  mutations:{
    FindFarmerByUid(state,value){
      state.loginFarmer = value
    },
    FindAllFarmer(state,value){
      state.allFarmer = value
    }
  },
  state:{
    loginFarmer:{},
    allFarmer:[],
  }
};

Vue.use(Vuex)

export default new Vuex.Store({
  modules:{
    user:user,
    crop:crop,
    disease:disease,
    expert:expert,
    question:question,
    farmer:farmer
  }
})